package com.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
    public CorsConfig() {

    }

    @Bean
    public CorsFilter corsFilter () {
        //1、添加cors配置信息
        CorsConfiguration config = new CorsConfiguration();
        //配置可访问白名单
        config.addAllowedOriginPattern("*");
        //设置是否发送Cookie信息
        config.setAllowCredentials(true);
        //设置允许请求的方式，此处默认设置所有请求方式都可以通过
        config.addAllowedMethod("*");
        //设置允许的请求头
        config.addAllowedHeader("*");

        //2、为URL添加映射路径
        UrlBasedCorsConfigurationSource corsCourse = new UrlBasedCorsConfigurationSource();
        //设置放行映射路径，此处/**代表放行所有的请求
        corsCourse.registerCorsConfiguration("/**",config);

        //3、返回重新定义的好的corsCourse
        return new CorsFilter(corsCourse);
    }
}
